package com.winbomb.kingcard.screens;

import com.winbomb.kingcard.Assets;
import com.winbomb.kingcard.IBattle;
import com.winbomb.kingcard.ICard;
import com.winbomb.kingcard.IState;
import com.winbomb.kingcard.base.AnimManager;
import com.winbomb.kingcard.base.BattleState;
import com.winbomb.kingcard.base.Card;
import com.winbomb.kingcard.base.GameLogic;
import com.winbomb.kingcard.framework.Game;
import com.winbomb.kingcard.framework.Screen;
import com.winbomb.kingcard.framework.Input.TouchEvent;
import com.winbomb.kingcard.framework.impl.TouchArea;
import com.winbomb.kingcard.screens.util.Render;
import com.winbomb.kingcard.utils.GameUtils;

public class BattleScreen extends Screen {

	/** 战斗时上面和下面的卡片区 */
	private static final TouchArea upCardsArea = new TouchArea(85, 77, 310, 72);
	private static final TouchArea downCardsArea = new TouchArea(85, 161, 310, 72);

	/** 战斗时的功能按钮 */
	private static final TouchArea btnNoGeneral = new TouchArea(250, 261, 71, 36); // 放弃叫阵主将设置
	private static final TouchArea btnSetGeneral = new TouchArea(154, 261, 71, 36); // 设置叫阵主将
	private static final TouchArea btnFinishArray = new TouchArea(201, 261, 78, 37); // 完成布阵
	private static final TouchArea btnEndBattle = new TouchArea(201, 261, 78, 37); // 结束战斗

	private GameLogic gameImpl;
	private AnimManager animMgr;
	private IBattle battle;

	/** 列阵时用来记录用户选择的交换和被交换的牌 */
	private int swapSrc = -1;

	public BattleScreen(Game game, GameLogic gameImpl) {
		super(game);

		this.gameImpl = gameImpl;
		this.animMgr = gameImpl.getAnimManager();
		this.battle = gameImpl.getBattle();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void present(float deltaTime) {
		Render.drawBattleField(gameImpl);

		IState gameState = gameImpl.getState();

		// 如果在排兵布阵阶段,绘制选中的牌
		if ((gameState == BattleState.OFFENSIVE_ARRAY || gameState == BattleState.DEFENSIVE_ARRAY) && swapSrc != -1) {
			g.drawPixmap(Assets.marks, 85 + swapSrc * 64, 182, 0, 0, 50, 70);
		}

		g.drawText("STATE:" + gameImpl.getState(), 150, 40);

		animMgr.play(deltaTime * 1000);
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(float deltaTime) {

		gameImpl.getCurrPlayer().elapse(deltaTime);
		gameImpl.step(deltaTime);
		gameImpl.getCurrPlayer().handle();

		if (!gameImpl.isPaused() && gameImpl.getCurrPlayer().isDone()) {
			gameImpl.nextStep();
		}

		updateTouchEvents();
		for (TouchEvent event : touchEvents) {
			if (event.type != TouchEvent.TOUCH_UP) {
				continue;
			}

			if (!gameImpl.getCurrPlayer().isHuman() || gameImpl.isPaused()) {
				continue;
			}

			IState gameState = gameImpl.getState();
			if ((gameState == BattleState.INVADER_SET_GENERAL || gameState == BattleState.GUARDER_SET_GENERAL)) {

				ICard[] atkCards = battle.getAtkCards();

				// 选择主将叫阵
				if (downCardsArea.isBound(event)) {
					int idx = getBattleCardIndex(event.x);
					if (idx == -1) {
						return;
					}

					if (atkCards[idx] != null && atkCards[idx].getPoint() > 10) {
						GameUtils.clearFlags(atkCards, Card.IS_GENERAL);
						atkCards[idx].setFlag(Card.IS_GENERAL);
					}

				} else if (btnSetGeneral.isBound(event)) {
					if (GameUtils.hasFlag(atkCards, Card.IS_GENERAL)) {
						gameImpl.nextStep();
					}
				} else if (btnNoGeneral.isBound(event)) {
					gameImpl.nextStep();
				}

			} else if (gameState == BattleState.OFFENSIVE_ARRAY || gameState == BattleState.DEFENSIVE_ARRAY) {

				// 排兵布阵
				if (downCardsArea.isBound(event)) {
					int swapDest = getBattleCardIndex(event.x);
					if (swapDest == -1) {
						return;
					}

					// 查看之前是否已经选了一张牌,如果选了则交换,如果没选则设置这张牌为选中
					ICard[] cards = battle.getAtkCards();
					if (swapSrc != -1) {
						GameUtils.swapCards(cards, swapSrc, swapDest);
						swapSrc = -1;
					} else {
						swapSrc = swapDest;
					}

					return;
				}

				if (btnFinishArray.isBound(event)) {
					gameImpl.nextStep();
				}

			} else if (gameState == BattleState.SELECT_ATK_CARD) {

				// 选择出击的牌
				if (downCardsArea.isBound(event)) {
					int idx = getBattleCardIndex(event.x);
					ICard[] atkCards = battle.getAtkCards();
					if (idx == -1 || atkCards[idx] == null || !atkCards[idx].testFlag(Card.IS_UNOPEN_CARD)) {
						return;
					} else {
						atkCards[idx].setSelected(true);
					}

					gameImpl.nextStep();
				}

			} else if (gameState == BattleState.SELECT_ATK_TARGET) {

				// 选择进攻对象
				if (upCardsArea.isBound(event)) {
					int idx = getBattleCardIndex(event.x);

					ICard[] defCards = battle.getDefCards();
					if (idx == -1 || defCards[idx] == null || defCards[idx].testFlag(Card.IS_DEFEATED)) {
						return;
					}

					battle.setCurrAtkTarget(idx);
					gameImpl.nextStep();
				}

			} else if (gameState == BattleState.BATTLE_END) {
				if (btnEndBattle.isBound(event)) {
					gameImpl.nextStep();
					game.setScreen(new GameScreen(game, gameImpl));
				}
			}
		}

	}

	/**
	 * 获取战场上牌的位置坐标,从左到右依次为0,1,2,3,4.如果没有点中牌,则返回-1.
	 * 
	 * @param x
	 * @return
	 */
	private static int getBattleCardIndex(int x) {
		return (x - 85) / 64;
	}

}
